package com.qingke.easyjava.jdbc.command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.easyjava.jdbc.command.impl.AcceptCommand;
import com.qingke.easyjava.jdbc.command.impl.AnswerCommand;
import com.qingke.easyjava.jdbc.command.impl.AskCommand;
import com.qingke.easyjava.jdbc.command.impl.ListCommand;
import com.qingke.easyjava.jdbc.command.impl.ScoreCommand;
import com.qingke.easyjava.jdbc.pojo.Player;

public class PlayerCommandFactory extends SystemCommandFactory{

	private Player player;
	public PlayerCommandFactory(Player player) {
		this.player = player;
	}
	
private static Map<CommandCode,Class<? extends Command>> commandMap = new HashMap<>();
	
	static { //静态代码块在类加载的时候进行一些静态操作
		commandMap.put(CommandCode.ASK, AskCommand.class);
		commandMap.put(CommandCode.ANSWER, AnswerCommand.class);
		commandMap.put(CommandCode.LIST, ListCommand.class);
		commandMap.put(CommandCode.SCORE, ScoreCommand.class);
		commandMap.put(CommandCode.ACCEPT, AcceptCommand.class);
		
	}
	
	@Override
	public Command buildCommand(CommandCode cmd) {

		Class<? extends Command> clzz = commandMap.get(cmd);
		
		if(clzz == null) {
			return super.buildCommand(cmd);
		}
		
		try {
			Constructor<? extends Command> c = clzz.getConstructor(Player.class);
			
			Command command = c.newInstance(player);
			return command;
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}
}
